﻿Imports Crystal.CLL
Imports CrystalDataHub.BLL.CrystalDataHub.BLL

Public Class ChangeMappingLog
    Inherits BaseForm

#Region "Fields"
    Private mclsChangeMappingLogManager As New ChangeMappingLogManager
    Private mintProjectID As Integer
    Private mblnAnyDataChange As Boolean = False
    Private mstrScreenName As String = "Change Mapping Log"
#End Region

#Region "Constructors"
    Sub New(ByVal pintProjectID As Integer)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        mintProjectID = pintProjectID
    End Sub
#End Region

#Region "On Load"

    Private Sub ChangeMappingLog_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If mblnAnyDataChange Then
            'Confirm from the user
            Dim ldrSaveExit As DialogResult
            ldrSaveExit = MessageBox.Show(MessageManager.GetMessage(DBResult.EDS_CHANGELOG_EXIT.ToString), MessageManager.GetMessage("CRYSTAL"), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
            If ldrSaveExit = DialogResult.Yes Then
                btnSaveAndExit.PerformClick()
            ElseIf ldrSaveExit = DialogResult.Cancel Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub ChangeMappingLog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CallOnLoad()
    End Sub

#End Region

#Region "Events"

#Region "Form Events"
    Private Sub ChangeMappingLog_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub
#End Region

#Region "Grid Events"

    Private Sub grdChangeLog_BeforeRowUpdate(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.CancelableRowEventArgs) Handles grdChangeLog.BeforeRowUpdate
        CommonCLL.CheckUpdateStatus(mblnAnyDataChange, grdChangeLog)
    End Sub

    Private Sub grdChangeLog_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdChangeLog.LostFocus
        CommonCLL.CheckUpdateStatus(mblnAnyDataChange, grdChangeLog)
    End Sub

    Private Sub grdChangeLog_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grdChangeLog.MouseClick
        Dim status As Boolean = False
        If (grdChangeLog.ActiveCell Is Nothing) Then Exit Sub
        If (grdChangeLog.ActiveRow Is Nothing) Then Exit Sub
        If (grdChangeLog.ActiveCell.Column.Header.Caption = "Review") Then
            status = grdChangeLog.ActiveCell.Row.GetCellValue(grdChangeLog.ActiveCell.Column)
            If (status = True) Then
                grdChangeLog.ActiveRow.Cells("Review").Value = False
            Else
                grdChangeLog.ActiveRow.Cells("Review").Value = True
            End If
            CType(grdChangeLog.DataSource, DataTable).AcceptChanges()
        End If
    End Sub

#End Region

#Region "Button Events"

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        SaveLog()
        mblnAnyDataChange = False
    End Sub

    Private Sub btnSaveAndExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAndExit.Click
        'SaveLog()
        Me.Close()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

#End Region

#End Region

#Region "Procedures"

    Private Sub CallOnLoad()
        SetOnLoadControls()
        LoadGridData()
    End Sub

    Public Sub SetOnLoadControls()
        Me.Text = TitleManager.GetTitle("ChangeMappingLog")
        lblLog.Text = MessageManager.GetMessage(DBResult.EDS_CHANGED_MAPPING_LOG.ToString)
        grdChangeLog.UpdateMode = Infragistics.Win.UltraWinGrid.UpdateMode.OnRowChangeOrLostFocus
    End Sub

    ''' <summary>
    ''' Load the Change Mapping Log
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadGridData()
        Dim ldtChangeLog As New DataTable
        Dim ldrChangeLog As IDataReader
        Try
            ldrChangeLog = mclsChangeMappingLogManager.GetChangeMappingLog(mintProjectID)
            ldtChangeLog.Load(ldrChangeLog)
            CommonCLL.ChangeDataReaderDataTable(ldtChangeLog)
            grdChangeLog.DataSource = ldtChangeLog
            grdChangeLog.Rows.Band.Columns("ChangeMapLogID").Hidden = True
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, mintProjectID)
        Finally
            ldtChangeLog = Nothing
            ldrChangeLog = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' Save the log changes
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SaveLog()
        Dim lstrData As String
        Dim ldtChangeLog As New DataTable
        ldtChangeLog = CType(grdChangeLog.DataSource, DataTable)
        Dim lintColCount As Int16 = 0
        Dim lintArrColCount As Int16 = 0
        Dim larrExcludingCols(1) As String
        Try
            While lintColCount < ldtChangeLog.Columns.Count
                If ldtChangeLog.Columns(lintColCount).ColumnName <> "ChangeMapLogID" AndAlso ldtChangeLog.Columns(lintColCount).ColumnName <> "Review" Then
                    larrExcludingCols(lintArrColCount) = ldtChangeLog.Columns(lintColCount).ColumnName
                    ReDim Preserve larrExcludingCols(lintArrColCount + 1)
                    lintArrColCount += 1
                End If
                lintColCount += 1
            End While
            ReDim Preserve larrExcludingCols(lintArrColCount - 1)
            lstrData = CommonCLL.GetXMlFromDataTable(ldtChangeLog, larrExcludingCols)
            mclsChangeMappingLogManager.UpdateChangeMapLogStatus(lstrData)
            Me.ShowMessageBox(DBResult.EDS_CHANGELOG_UPDATED, BaseClass.Capitalize("eSource Change Log"))
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, mintProjectID)
        Finally
            ldtChangeLog = Nothing
            larrExcludingCols = Nothing
        End Try
    End Sub

    'Private Sub CheckUpdateStatus()
    '    If grdChangeLog.ActiveRow IsNot Nothing Then
    '        If grdChangeLog.ActiveRow.DataChanged AndAlso mblnAnyDataChange = False Then
    '            mblnAnyDataChange = True
    '        End If
    '    End If
    'End Sub

#End Region

End Class